package cn.csc.module.shop.service.hotsearch;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.shop.controller.admin.hotsearch.vo.ShopHotSearchCreateReqVO;
import cn.csc.module.shop.controller.admin.hotsearch.vo.ShopHotSearchExportReqVO;
import cn.csc.module.shop.controller.admin.hotsearch.vo.ShopHotSearchPageReqVO;
import cn.csc.module.shop.controller.admin.hotsearch.vo.ShopHotSearchUpdateReqVO;
import cn.csc.module.shop.convert.hotsearch.ShopHotSearchConvert;
import cn.csc.module.shop.dal.dataobject.hotsearch.ShopHotSearchDO;
import cn.csc.module.shop.dal.mysql.hotsearch.ShopHotSearchMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.shop.enums.ErrorCodeConstants.SHOP_HOT_SEARCH_NOT_EXISTS;

/**
 * 热搜 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class ShopHotSearchServiceImpl implements ShopHotSearchService {

    @Resource
    private ShopHotSearchMapper hotSearchMapper;

    @Override
    public BigInteger createHotSearch(ShopHotSearchCreateReqVO createReqVO) {
        // 插入
        ShopHotSearchDO hotSearch = ShopHotSearchConvert.INSTANCE.convert(createReqVO);
        hotSearchMapper.insert(hotSearch);
        // 返回
        return hotSearch.getId();
    }

    @Override
    public void updateHotSearch(ShopHotSearchUpdateReqVO updateReqVO) {
        // 校验存在
        validateHotSearchExists(updateReqVO.getId());
        // 更新
        ShopHotSearchDO updateObj = ShopHotSearchConvert.INSTANCE.convert(updateReqVO);
        hotSearchMapper.updateById(updateObj);
    }

    @Override
    public void deleteHotSearch(BigInteger id) {
        // 校验存在
        validateHotSearchExists(id);
        // 删除
        hotSearchMapper.deleteById(id);
    }

    private void validateHotSearchExists(BigInteger id) {
        if (hotSearchMapper.selectById(id) == null) {
            throw exception(SHOP_HOT_SEARCH_NOT_EXISTS);
        }
    }

    @Override
    public ShopHotSearchDO getHotSearch(BigInteger id) {
        return hotSearchMapper.selectById(id);
    }

    @Override
    public List<ShopHotSearchDO> getHotSearchList(Collection<BigInteger> ids) {
        return hotSearchMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopHotSearchDO> getHotSearchPage(ShopHotSearchPageReqVO pageReqVO) {
        return hotSearchMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopHotSearchDO> getHotSearchList(ShopHotSearchExportReqVO exportReqVO) {
        return hotSearchMapper.selectList(exportReqVO);
    }

}
